package net.yxsoft.kit.rongzer;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.yxsoft.kit.rongzer.RSAEncrypt;
import net.yxsoft.kit.rongzer.RSASignature;
import net.yxsoft.kit.rongzer.Util;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import rapid.util.PathKit;

public class MainTest {
	public static void main(String[] args) throws Exception {



		// CA证书路径--需要生成自己的CA证书
		String pfxPath = PathKit.getRootClassPath()+"/trustedata.pfx";
		// 获取请求方公钥和私钥
		String publicKey = Util.getHexString(RSAEncrypt.loadPublicKeyByFile(pfxPath, "trustedata").getEncoded());
		String privateKey = Util.getHexString(RSAEncrypt.loadPrivateKeyByFile(pfxPath, "trustedata").getEncoded());

		System.out.println("puiblicKey : " + publicKey);
		System.out.println("privateKey : " + privateKey);

		// 服务方公钥
		String BPublicKey = "30819F300D06092A864886F70D010101050003818D0030818902818100B4038935C37EAFB9D233A8257BC3D19C0E7717C99C4EBFEDA618AC4C65D8548F814595C957CF9DE55D94C8B6C68C4C47AE5759924F72F9009DDD23DD58F550B5332D6B7C8A0D44DD3140C66CE2D67EB47FBD2BBC78469EB1BF94DC89808F2C1F5597B44AF948F985ACBC3AF43BA470586775A557296A8E73F57FDF30B7EE5BFD0203010001";

		// 参数加密
		String xm = Util
				.getHexString(RSAEncrypt.encrypt(RSAEncrypt.loadPublicKeyByStr(BPublicKey), "吴**".getBytes("UTF-8")));

		// 参数加密
		String sfzhm = Util.getHexString(
				RSAEncrypt.encrypt(RSAEncrypt.loadPublicKeyByStr(BPublicKey), "320106********1234".getBytes("UTF-8")));

		// 参数封装
		Map<String, String> paramfromA = new HashMap<String, String>();
		paramfromA.put("xm", xm);
		paramfromA.put("sfzhm", sfzhm);
		// 对参数进行排序，以防止传输过程中参数顺序出现错乱
		paramfromA = Util.sortMapByKey(paramfromA);
		// 参数拼接
		String paramfromAStr = Util.getParams(paramfromA);

		// 参数签名
		String sign = RSASignature.sign(paramfromAStr, privateKey);

		// 定义一个Http连接器
		HttpClient httpClient = HttpClients.createDefault();
		HttpPost post = new HttpPost("http://172.16.209.36/cfscms/sipsd/service/ThirdService/QueryXLBySFZHM");
		// 设置请求参数
		List<NameValuePair> data = new ArrayList<NameValuePair>();
		data.add(new BasicNameValuePair("xm", xm));
		data.add(new BasicNameValuePair("sfzhm", sfzhm));
		data.add(new BasicNameValuePair("sign", sign));
		data.add(new BasicNameValuePair("publicKey", publicKey));
		String urlStr = URLEncoder.encode("IqVLvQ/QanLHOtKmMgSR7Xjo+/TVQ3r8/hF5wQEshRjGADS7UVX2JGepIbuyY4POHGamUchwUKPLMcb1LZdkBec61PlF5SV3ELj2VkEVTDcnlukjczil6A==");
		data.add(new BasicNameValuePair("token", urlStr));

		// UrlEncodedFormEntity类创建的对象可以模拟传统的HTML表单传送POST请求中的参数
		UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(data);
		post.setEntity(formEntity);

		// 执行post请求
		HttpResponse response = httpClient.execute(post);
		// 打印出响应的内容
		HttpEntity entity = response.getEntity();
		String result = EntityUtils.toString(entity, "utf-8");
		System.out.println(result);

		JSONObject object = JSONObject.parseObject(result);
		if (object.getString("fhdm").equals("10")) {
			// 结果验签
			boolean isSign = RSASignature.doCheck(object.getString("data"), object.getString("sign"), BPublicKey);
			System.out.println("验签结果 : " + isSign);
			if (isSign) {
				String restr = new String(RSAEncrypt.decrypt(RSAEncrypt.loadPrivateKeyByStr(privateKey),
						Util.hexStringToBytes(object.getString("data"))), "UTF-8");
				System.out.println("解密结果 : " + restr);
			} else {
				System.out.println("验签失败 ");
			}
		}
	}
}
